#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/mman.h>
#include<sys/wait.h>
#include<string.h>
#include<sys/types.h>
#include<semaphore.h>


int main(int argc, char const *argv[])
{   
    char *sem_name = "sem_pv";
    //创建共享内存信号量
    int sem_fd = open(sem_name, O_CREAT | O_RDWR, 0666);
    ftruncate(sem_fd, sizeof(sem_t));
    sem_t *sem =mmap(&sem_fd, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, sem_fd, 0);
    sem_init(sem, 1, 0);

    int pid=fork();
    if (pid==-1)
    {
        perror("fork");
        
    }else if (pid==0)
    {
        sleep(1);
        printf("child process\n");
        sem_post(sem);
    }else{
        sem_wait(sem);
        printf("parent process\n");
    }

    if (pid>0)
    {
        sem_destroy(sem);
        munmap(sem, sizeof(sem_t));
        shm_unlink(sem_name);
    }
    close(sem_fd);
    
    
    

}
